package com.example.showpicture1.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.example.showpicture1.R;

import java.util.List;

/**
 * author : Lele
 * date   : 2022/7/13 9:28
 * desc   :
 * version: 1.0
 */
public class ViewPagerAdapter extends PagerAdapter {
    private final Context mContext;
    private final List<String> mList;

    public ViewPagerAdapter(Context mContext, List<String> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    //创建条目
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bigimage, null);
        //绑定PhotoView控件
        final PhotoView imageView = view.findViewById(R.id.iv_item_bigimage);
        //将文件中的图片加载到imageView里去
        Glide.with(mContext).load("/storage/emulated/0/ImageForShow/" + mList.get(position)).into(imageView);
        //设置图片可放大缩小
        imageView.enable();
        container.addView(view);
        return view;
    }

    //销毁条目
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}
